package test05;

import java.util.*;

// 1. 抽象产品接口 - 语音识别服务
interface SpeechService {
    String recognize(String audio);
    String getProvider();
    List<String> getCapabilities();
}

// 2. 抽象产品接口 - 图像分析服务
interface ImageService {
    String analyze(String image);
    String getProvider();
    List<String> getFeatures();
}

// 3. 具体产品 - 阿里云语音服务
class AliyunSpeechService implements SpeechService {
    @Override
    public String recognize(String audio) {
        return "阿里云语音识别结果: [" + audio + "] - 支持4K音频, 高精度识别";
    }

    @Override
    public String getProvider() {
        return "阿里云";
    }

    @Override
    public List<String> getCapabilities() {
        return Arrays.asList("4K音频支持", "实时转写", "方言识别", "高并发处理");
    }
}

// 4. 具体产品 - 阿里云图像服务
class AliyunImageService implements ImageService {
    @Override
    public String analyze(String image) {
        return "阿里云图像分析结果: [" + image + "] - 物体检测, 场景识别, 支持大规模分析";
    }

    @Override
    public String getProvider() {
        return "阿里云";
    }

    @Override
    public List<String> getFeatures() {
        return Arrays.asList("物体检测", "场景识别", "人脸比对", "OCR", "高并发配置");
    }
}

// 5. 具体产品 - 腾讯云语音服务
class TencentSpeechService implements SpeechService {
    @Override
    public String recognize(String audio) {
        return "腾讯云语音识别结果: [" + audio + "] - 低延迟, 高准确率, 支持长音频";
    }

    @Override
    public String getProvider() {
        return "腾讯云";
    }

    @Override
    public List<String> getCapabilities() {
        return Arrays.asList("低延迟识别", "长音频处理", "实时转写", "多语种支持");
    }
}

// 6. 具体产品 - 腾讯云图像服务
class TencentImageService implements ImageService {
    @Override
    public String analyze(String image) {
        return "腾讯云图像分析结果: [" + image + "] - 低延迟响应, 精准物体识别";
    }

    @Override
    public String getProvider() {
        return "腾讯云";
    }

    @Override
    public List<String> getFeatures() {
        return Arrays.asList("低延迟检测", "物体识别", "场景分析", "图像增强");
    }
}

// 7. 具体产品 - AWS语音服务
class AWSSpeechService implements SpeechService {
    @Override
    public String recognize(String audio) {
        return "AWS语音识别结果: [" + audio + "] - 多语言支持, 自定义词汇表";
    }

    @Override
    public String getProvider() {
        return "AWS";
    }

    @Override
    public List<String> getCapabilities() {
        return Arrays.asList("多语言识别", "自定义词汇", "实时转写", "说话人分离");
    }
}

// 8. 具体产品 - AWS图像服务
class AWSImageService implements ImageService {
    @Override
    public String analyze(String image) {
        return "AWS图像分析结果: [" + image + "] - 高级物体检测, 情感分析";
    }

    @Override
    public String getProvider() {
        return "AWS";
    }

    @Override
    public List<String> getFeatures() {
        return Arrays.asList("高级物体检测", "场景理解", "情感分析", "文本识别");
    }
}

// 9. 抽象工厂
interface AIServiceFactory {
    SpeechService createSpeechService();
    ImageService createImageService();
    String getProviderName();
}

// 10. 具体工厂 - 阿里云工厂
class AliyunFactory implements AIServiceFactory {
    @Override
    public SpeechService createSpeechService() {
        System.out.println("创建阿里云语音服务 - 支持4K音频和高并发");
        return new AliyunSpeechService();
    }

    @Override
    public ImageService createImageService() {
        System.out.println("创建阿里云图像服务 - 支持大规模分析");
        return new AliyunImageService();
    }

    @Override
    public String getProviderName() {
        return "阿里云";
    }
}

// 11. 具体工厂 - 腾讯云工厂
class TencentFactory implements AIServiceFactory {
    @Override
    public SpeechService createSpeechService() {
        System.out.println("创建腾讯云语音服务 - 低延迟高准确率");
        return new TencentSpeechService();
    }

    @Override
    public ImageService createImageService() {
        System.out.println("创建腾讯云图像服务 - 低延迟响应");
        return new TencentImageService();
    }

    @Override
    public String getProviderName() {
        return "腾讯云";
    }
}

// 12. 具体工厂 - AWS工厂
class AWSFactory implements AIServiceFactory {
    @Override
    public SpeechService createSpeechService() {
        System.out.println("创建AWS语音服务 - 多语言和自定义词汇");
        return new AWSSpeechService();
    }

    @Override
    public ImageService createImageService() {
        System.out.println("创建AWS图像服务 - 高级分析和情感识别");
        return new AWSImageService();
    }

    @Override
    public String getProviderName() {
        return "AWS";
    }
}

// 13. 多云AI服务平台
class CloudAIPlatform {
    private AIServiceFactory currentFactory;

    public CloudAIPlatform(AIServiceFactory factory) {
        this.currentFactory = factory;
        System.out.println("初始化AI平台，使用提供商: " + factory.getProviderName());
    }

    public void switchProvider(AIServiceFactory factory) {
        this.currentFactory = factory;
        System.out.println("已切换到提供商: " + factory.getProviderName());
    }

    public String processAudio(String audio) {
        SpeechService speechService = currentFactory.createSpeechService();
        System.out.println("使用 " + speechService.getProvider() + " 处理音频");
        System.out.println("服务能力: " + speechService.getCapabilities());
        return speechService.recognize(audio);
    }

    public String processImage(String image) {
        ImageService imageService = currentFactory.createImageService();
        System.out.println("使用 " + imageService.getProvider() + " 处理图像");
        System.out.println("服务特性: " + imageService.getFeatures());
        return imageService.analyze(image);
    }

    public String getCurrentProvider() {
        return currentFactory.getProviderName();
    }

    // 批量处理 - 展示产品族一致性
    public void processMultiModal(String audio, String image) {
        System.out.println("\n=== 多模态处理 ===");
        System.out.println("当前提供商: " + getCurrentProvider());

        SpeechService speechService = currentFactory.createSpeechService();
        ImageService imageService = currentFactory.createImageService();

        // 确保同一厂商的服务兼容性
        if (!speechService.getProvider().equals(imageService.getProvider())) {
            throw new IllegalStateException("服务提供商不一致!");
        }

        System.out.println("语音服务: " + speechService.recognize(audio));
        System.out.println("图像服务: " + imageService.analyze(image));
        System.out.println("✅ 同一厂商服务兼容性验证通过");
    }
}

// 14. 客户端代码
public class MultiCloudAIPlatform {
    public static void main(String[] args) {
        System.out.println("=== 多云AI服务平台 ===\n");

        // 创建不同云厂商的工厂
        AIServiceFactory aliyunFactory = new AliyunFactory();
        AIServiceFactory tencentFactory = new TencentFactory();
        AIServiceFactory awsFactory = new AWSFactory();

        // 初始化平台使用阿里云
        CloudAIPlatform platform = new CloudAIPlatform(aliyunFactory);

        // 测试阿里云服务
        System.out.println("\n--- 测试阿里云服务 ---");
        String audioResult = platform.processAudio("会议录音.wav");
        System.out.println("结果: " + audioResult);

        String imageResult = platform.processImage("场景照片.jpg");
        System.out.println("结果: " + imageResult);

        // 测试多模态处理
        platform.processMultiModal("语音指令", "场景图像");

        // 切换到腾讯云
        System.out.println("\n--- 切换到腾讯云 ---");
        platform.switchProvider(tencentFactory);

        System.out.println("\n--- 测试腾讯云服务 ---");
        audioResult = platform.processAudio("通话录音.mp3");
        System.out.println("结果: " + audioResult);

        imageResult = platform.processImage("物体图片.png");
        System.out.println("结果: " + imageResult);

        platform.processMultiModal("语音输入", "图像输入");

        // 切换到AWS
        System.out.println("\n--- 切换到AWS ---");
        platform.switchProvider(awsFactory);

        System.out.println("\n--- 测试AWS服务 ---");
        audioResult = platform.processAudio("英文演讲.wav");
        System.out.println("结果: " + audioResult);

        imageResult = platform.processImage("情感图片.jpg");
        System.out.println("结果: " + imageResult);

        platform.processMultiModal("多语言音频", "复杂场景图像");

        // 演示扩展性
        demonstrateExtensibility();
    }

    private static void demonstrateExtensibility() {
        System.out.println("\n=== 扩展性演示 ===");

        // 模拟新增华为云服务
        System.out.println("若要新增华为云服务，需要:");
        System.out.println("1. ✅ 创建 HuaweiSpeechService (实现 SpeechService)");
        System.out.println("2. ✅ 创建 HuaweiImageService (实现 ImageService)");
        System.out.println("3. ✅ 创建 HuaweiFactory (实现 AIServiceFactory)");
        System.out.println("4. ❌ 无需修改现有任何代码");
        System.out.println("5. ❌ 无需修改客户端代码");
        System.out.println("6. ❌ 无需修改平台核心逻辑");
        System.out.println("\n✅ 完全符合开闭原则!");

        // 模拟新增自然语言处理服务
        System.out.println("\n若要新增自然语言处理服务，需要:");
        System.out.println("1. ✅ 创建 NLPService 接口");
        System.out.println("2. ✅ 在各厂商中实现具体NLPService");
        System.out.println("3. ✅ 在AIServiceFactory中新增createNLPService()方法");
        System.out.println("4. ❌ 此步骤会破坏开闭原则，但这是接口扩展的正常情况");
        System.out.println("5. ✅ 具体工厂实现新方法，客户端可选择使用新服务");
    }
}